package org.yelongframework.pdm.xml;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.yelongframework.pdm.PdmColumn;
import org.yelongframework.pdm.PdmTable;
import org.yelongframework.util.Assert;

/**
 * XML实现
 * 
 * @since 1.0.0
 */
public class DefaultXmlPdmTable implements PdmTable {

	private static final long serialVersionUID = -3159047720654243730L;

	private final OTable oTable;

	public DefaultXmlPdmTable(OTable oTable) {
		Assert.notNull(oTable, "oTable cannot be null");
		this.oTable = oTable;
	}

	@Override
	public String getId() {
		return oTable.getId();
	}

	@Override
	public void setId(String id) {
		oTable.setId(id);
	}

	@Override
	public String getObjectId() {
		return oTable.getaObjectId();
	}

	@Override
	public void setObjectId(String objectId) {
		oTable.setaObjectId(objectId);
	}

	@Override
	public String getCreator() {
		return oTable.getaCreator();
	}

	@Override
	public void setCreator(String creator) {
		oTable.setaCreator(creator);
	}

	@Override
	public String getCreationDateStr() {
		return oTable.getaCreationDate();
	}

	@Override
	public void setCreationDateStr(String creationDateStr) {
		oTable.setaCreationDate(creationDateStr);
	}

	@Override
	public Date getCreationDate() {
		String creationDateStr = getCreationDateStr();
		if (StringUtils.isBlank(creationDateStr)) {
			return null;
		}
		return new Date(Long.valueOf(creationDateStr));
	}

	@Override
	public void setCreationDate(Date creationDate) {
		setCreationDateStr(null == creationDate ? null : creationDate.getTime() + "");
	}

	@Override
	public String getModifier() {
		return oTable.getaModifier();
	}

	@Override
	public void setModifier(String modifier) {
		oTable.setaModifier(modifier);
	}

	@Override
	public String getModificationDateStr() {
		return oTable.getaModificationDate();
	}

	@Override
	public void setModificationDateStr(String modificationDateStr) {
		oTable.setaModificationDate(modificationDateStr);
	}

	@Override
	public Date getModificationDate() {
		String modificationDateStr = getModificationDateStr();
		if (StringUtils.isBlank(modificationDateStr)) {
			return null;
		}
		return new Date(Long.valueOf(modificationDateStr));
	}

	@Override
	public void setModificationDate(Date modificationDate) {
		oTable.setaModificationDate(null == modificationDate ? null : modificationDate.getTime() + "");
	}

	@Override
	public String getName() {
		return oTable.getaName();
	}

	@Override
	public void setName(String name) {
		oTable.setaName(name);
	}

	@Override
	public String getCode() {
		return oTable.getaCode();
	}

	@Override
	public void setCode(String code) {
		oTable.setaCode(code);
	}

	@Override
	public String getComment() {
		return oTable.getaComment();
	}

	@Override
	public void setComment(String comment) {
		oTable.setaComment(comment);
	}

	@Override
	public List<PdmColumn> getColumns() {
		return oTable.getcColumns().getoColumnList().stream().map(x -> new DefaultXmlPdmColumn(oTable, x))
				.collect(Collectors.toList());
	}

	@Override
	public void setColumns(List<PdmColumn> columns) {
		throw new UnsupportedOperationException();
	}

	@Override
	public List<PdmColumn> getPrimaryKeys() {
		CPrimaryKey cPrimaryKey = oTable.getcPrimaryKey();
		List<String> keyRefList = cPrimaryKey.getoKeyRefList();
		if (CollectionUtils.isEmpty(keyRefList)) {
			return Collections.emptyList();
		}
		List<OColumn> primaryOColumnList = new ArrayList<>();
		CColumns cColumns = oTable.getcColumns();
		List<OColumn> oColumnList = cColumns.getoColumnList();
		CKeys cKeys = oTable.getcKeys();
		List<OKey> oKeyList = cKeys.getoKeyList();
		for (OKey oKey : oKeyList) {
			if (!keyRefList.contains(oKey.getId())) {
				continue;
			}
			CKeyColumns cKeyColumns = oKey.getcKeyColumns();
			List<String> oColumnRefList = cKeyColumns.getoColumnRefList();
			for (OColumn oColumn : oColumnList) {
				if (oColumnRefList.contains(oColumn.getId())) {
					primaryOColumnList.add(oColumn);
				}
			}
		}
		return primaryOColumnList.stream().distinct().map(x -> new DefaultXmlPdmColumn(oTable, x))
				.collect(Collectors.toList());
	}

	@Override
	public void setPrimaryKeys(List<PdmColumn> primaryKeys) {
		throw new UnsupportedOperationException();
	}

	@Override
	public String toString() {
		return oTable.toString();
	}

}
